<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="Id" prop="id" width="180"></el-table-column>
				<el-table-column label="楼层名字" prop="name" width="180"></el-table-column>
				<el-table-column label="楼层图片" prop="pic" width="300">
					<template #default="scope">
						<img v-if="scope.row.pic" :src="getfull(scope.row.pic)" width="50" height="24">
					</template>
				</el-table-column>
				<!-- <el-table-column label="性别" prop="sex" width="150"></el-table-column>
				<el-table-column label="邮箱" prop="email" width="250"></el-table-column>
				<el-table-column label="状态" prop="boolean" width="60">
					<template #default="scope">
						<sc-status-indicator v-if="scope.row.boolean" type="success"></sc-status-indicator>
						<sc-status-indicator v-if="!scope.row.boolean" pulse type="danger"></sc-status-indicator>
					</template>
				</el-table-column>
				<el-table-column label="评分" prop="num" width="150"></el-table-column> -->
				<el-table-column label="操作" fixed="right" align="right" width="500">
					<template #default="scope">
						<sc-upload-file class="adjustment" :apiObj="uploadApi" :data="{floor_id:scope.row.id}" :showFileList="false" :on-success="success" >
							<el-button  type="primary" size="small" icon="el-icon-upload">上传图片</el-button>
						</sc-upload-file>
						<el-button plain size="small" @click="table_show(scope.row)">查看楼层</el-button>
						<el-button type="primary" plain size="small" @click="table_edit(scope.row)">编辑楼层</el-button>		
						<el-button type="success" plain size="small" @click="table_add(scope.row)">新增房间</el-button>
						<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button plain type="danger" size="small">删除</el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

	<add-Room v-if="dialog.saveroom" ref="saveRoomDialog" @success="handleSaveSuccess" @closed="dialog.saveroom=false"></add-Room>

	<el-drawer v-model="dialog.info" :size="800" title="详细" direction="rtl" destroy-on-close>
		<info ref="infoDialog"></info>
	</el-drawer>

</template>

<script>
	import saveDialog from './save'
	import info from './info'
	import addRoom from './addRoom'

	export default {
		name: 'floor',
		components: {
			saveDialog,
			info,
			addRoom
		},
		data() {
			return {
				dialog:{
					save: false,
					info: false,
					saveroom: false
				},
				list: {
					apiObj: this.$API.floor.list
				},
				selection: [],
				baseUrl: process.env.VUE_APP_IMAGE_URL,
				uploadApi: this.$API.floor.upload,
			}
		},
		mounted() {
			console.log(this.baseUrl)
		},
		methods: {
			//窗口新增
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//窗口新增
			table_add(row){
				console.log(row)
				this.dialog.saveroom = true
				this.$nextTick(() => {
					this.$refs.saveRoomDialog.open().setData(row)
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.setData(row)
				})
			},
			//删除明细
			async table_del(row, index){
				var reqData = row.id
				var res = await this.$API.floor.delete.delete(reqData);
				if(res.code == 200){
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if(!confirmRes){
					return false
				}

				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				var res = await this.$API.floor.delete.delete(ids);
				if(res.code == 200){
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			},
			success (response){
				// this.$alert(`success函数钩子，可用于类似OCR返回信息，return false后阻止后续执行，回调参数打开控制台查看`, {
				// 	title: "提示",
				// 	type: "success"
				// })
				console.log(response);
				this.$refs.table.refresh()
				return false;
			},
			getfull(url) {
				return this.baseUrl + url;
			}
		
		}
	}
</script>

<style scoped>
.adjustment {
	display: inline-flex;
	height:24px;
	width:72px;
	border: 1px solid red;
	padding: 5px 11px;
	align-items: center;
	justify-content: center;
	margin-right: 24px;
	line-height: 12px; /* 与其他按钮保持一致 */
	vertical-align: middle;
}

</style>


